#include "carditem.h"
#include <QPropertyAnimation>
#include <QPicture>
#include <qmath.h>

#include "configmanager.h"

CardItem::CardItem(Card &&card, const QSizeF &size, QGraphicsItem *parent):
    QGraphicsObject(parent), m_size(size), m_card(card)
{
    flip_animation = new QPropertyAnimation(this, "angle");
}

QRectF CardItem::boundingRect() const // 如果结合动画则相当于用这片区域去覆盖上次的绘画，但不会影响其他图元
{
    return QRectF(0, 0, m_size.width(), m_size.height());
}

void CardItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
{
    painter->save();
    // 设置抗锯齿
    painter->setRenderHint(QPainter::Antialiasing);

    qreal halfWidth = this->m_size.width()/2;
    // 中轴坐标
    double cosRes = cos(M_PI*m_angle/180);
    QRectF rectF = QRectF(halfWidth*(1-abs(cosRes)), 0, abs(this->m_size.width()*cosRes), this->m_size.height());
    // 绘制卡片背景
    if (m_angle < 90) {
        // 绘制正面
        painter->drawPixmap(rectF.toRect(), QPixmap(QString(":/Sprites/Standard52Cards/%1.png").arg(m_card.getCardName())));
    }else{
        // 绘制背面
        painter->drawPixmap(rectF.toRect(), QPixmap(":/Sprites/Standard52Cards/Background.png"));
    }
    // 绘制边框
    painter->setPen(QPen(QColorConstants::Black, 0.5f));
    painter->setBrush(Qt::NoBrush);
    qreal radius = ConfigManager::instance().screenWidth/200;
    painter->drawRoundedRect(rectF, radius, radius);

    if(drawGrayMask){
        painter->setBrush(QColor(0, 0, 0, 20));
        painter->drawRoundedRect(rectF, radius, radius);
    }

    // 恢复 painter 状态
    painter->restore();
}

void CardItem::changeSelectStatus()
{
    if(isSelected){
        // qDebug() << "Before" << this->pos();
        // this->setPos(this->pos().x(), this->pos().y()+10.0f);
        QPropertyAnimation *animation = new QPropertyAnimation(this, "pos");
        animation->setDuration(20);

        animation->setStartValue(QPoint(this->pos().toPoint()));

        QPoint endPos(this->pos().x(), this->pos().y() + ConfigManager::instance().cardJumpHeight);
        animation->setEndValue(endPos);
        animation->start(QAbstractAnimation::DeleteWhenStopped);

        // 证明确实动画完毕后pos改变了
        // connect(animation, &QPropertyAnimation::finished, this, [=](){
        //     qDebug() << "After" << this->pos();
        // });

        this->isSelected = false;
    }else{ // 被选中往上弹起来
        QPropertyAnimation *animation = new QPropertyAnimation(this, "pos");
        animation->setDuration(20);

        animation->setStartValue(QPoint(this->pos().toPoint()));

        QPoint endPos(this->pos().x(), this->pos().y() - ConfigManager::instance().cardJumpHeight);
        animation->setEndValue(endPos);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
        this->isSelected = true;
    }
}

void CardItem::flip()
{
    flip_animation->setDuration(500);
    flip_animation->setEasingCurve(QEasingCurve::InOutQuad);

    // 设置动画的起始值和结束值
    if (!isFlipped) {
        flip_animation->setStartValue(0);
        flip_animation->setEndValue(180);
    } else {
        flip_animation->setStartValue(180);
        flip_animation->setEndValue(0);
    }
    isFlipped = !isFlipped;
    flip_animation->start();
    // connect(flip_animation, &QPropertyAnimation::finished, this, [=](){
    //     qDebug() << "After" << this->angle() << isFlipped;
    // });
}

